<?php
namespace app\admin\controller;

use app\admin\logic\UsersLogic;
use app\admin\model\AuthGroup;
use app\BaseController;
use think\facade\View;
use think\facade\Request;
//管理员列表
class Users extends BaseController
{
    public function userAdd()
    {
        //接值
        if (Request::isPost() ){
            $param = Request::param();
            //拼装添加数据data(逻辑层)
            $add = UsersLogic::addUserInfo($param);
            if (empty($add['author'])){
                $add['author'] = 2;
            }
            //执行添加操作(模型层)
            list($res,$msg) = \app\admin\model\Users::addUserInfo($add);
            if ($res){
                //成功
                $this->success($msg,url('usersList'));
            }else{
                //失败
                $this->error($msg);
            }
        }else{
            $list = AuthGroup::authGroup();
            View::assign('list',$list);
        }
        return View::fetch();
    }
    public function userEdit()
    {
        //查询
        if (Request::isPost()){
            //接值
            $post = Request::post();
            $get = Request::get();
            $edit = UsersLogic::editUserInfo($post);
            list($res,$msg) = \app\admin\model\Users::editUserInfo([],$edit);
            if ($res){
                if (isset($post['fh']) && !empty($post['fh'])){
                    $this->success($msg,url('',$get));
                }
                if (isset($post['jx']) && !empty($post['jx'])){
                    unset($get['id']);
                    $this->success($msg,url('usersList',$get),$res,3);
                }
            }else{
                $this->error($msg);
            }
        }else{
            //接值
            $get = Request::get();
            $list = \app\admin\model\Users::editUserInfo($get,[]);
            View::assign('list',$list);
            $auth = AuthGroup::authGroup();
            View::assign('auth',$auth);
        }
        return View::fetch();
    }
    public function usersList()
    {
        //接值
        $param = Request::param();
        //where条件封装
        $where = UsersLogic::reformData($param);
        //留值设置
        $data = UsersLogic::setData($param);
        View::assign($data);
        //查询
        $list = \app\admin\model\Users::getUserList($where,$param,$data);
        //分页
        $page = $list->render();
        View::assign(['list'=>$list,'page'=>$page]);
        return View::fetch();
    }


}